package com.hnjz.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.hnjz.sys.log.LoggerManager;

public class LoggerInterceptor implements HandlerInterceptor {
	
	private static final Logger log=Logger.getLogger(LoggerInterceptor.class);
	@Autowired
	private LoggerManager loggerManager;

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		String requestUri=request.getRequestURI();
		log.debug("request uri:"+requestUri);
		String contextPath=request.getContextPath();
		String url=requestUri.substring(contextPath.length());
		if(StringUtils.contains(url, "save")){
			loggerManager.saveLogger(url, "新增||编辑");
		}
		if(StringUtils.contains(url, "del")){
			loggerManager.saveLogger(url, "删除");
		}
		return true;
	}

}
